package com.wef.common.web;

/**
 * @FileName SessionFilter.java
 * @Summary  功能描述：
 * @Author   getdown
 * @Version  1.0
 * @Date     2009/07/31
 * @example  使用示例：略。
 */
import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gfa4j.web.SessionManager;
import com.gfa4j.wef.utils.CommonUtils;
import com.wef.common.service.UserInfo;

public class SessionFilter extends com.gfa4j.web.SessionFilter {
	/**
	 * 过滤器开始
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		SessionManager.setSession(req.getSession());
		String currentURI = req.getRequestURI();
		if (currentURI.contains(".dwr") || currentURI.contains(".do")) {
			String userCode = "Guest";
			try {
				userCode = UserInfo.getInstance().getUserCode();
			} catch (Exception e) {
			}
			if (CommonUtils.isEmpty(userCode)) {
				userCode = "Guest";
			}
		}
		if (urlAtLogin(currentURI)) {
			logger.debug("- Call login in method.");
			SessionManager.clearUserDetail();
		} else if (!isIgnored(currentURI, req)) {
			logger.debug("sessionID:" + session.getId());
			logger.debug("URL:" + currentURI);
			logger.debug("session.getAttribute('userDetail'):" + session.getAttribute("userDetail"));
			if (session.getAttribute("userDetail") == null) {
				logger.error("user has not login.");
				logger.error("currentURI: " + currentURI);
				req.setAttribute("requestURL", currentURI);
				res.sendRedirect(req.getContextPath() + "/login.jsp");
				return;
			} else if (currentURI.endsWith(".jsp")) {
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * 判断是否属于过滤范围
	 * 
	 * @param currentURL
	 * @param request
	 * @return
	 */
	protected boolean isIgnored(String currentURL, HttpServletRequest request) {
		boolean filterResult = false;

		StringBuffer prefix = new StringBuffer();
		prefix.append(request.getContextPath());
		prefix.append(prefix.toString().equals("/") ? "" : "/");
		String pre = prefix.toString();
		if (pre.equals(currentURL)) {
			return true;
		}
		String[] ignoreFiles = new String[] { "login.jsp", "dwrInterface.js", "__System.pageLoaded.dwr","__System.generateId.dwr", "engine.js", "util.js", "common.css", "login.jsp", "login.htm", ".gif", ".jpg", "menu.css", "style.css", "login.css", "versionChange.do", "index_pre_load.jsp", "i18n-zh_CN.js" };
		String[] ignorePaths = new String[] { "common", "images", "scripts", "styles", "widgets" };
		for (String ignoreFile : ignoreFiles) {
			filterResult = filterResult || currentURL.toLowerCase().endsWith(ignoreFile.toLowerCase());
		}
		for (String ignorePath : ignorePaths) {
			filterResult = filterResult || currentURL.toLowerCase().startsWith((pre + ignorePath).toLowerCase());
		}
		return filterResult;
	}

	protected boolean urlAtLogin(String uri) {
		return uri.toLowerCase().endsWith("loginservice.login.dwr");
	}

}
